package com.cxg.fairy.ec.pay;


import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialog;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.convenientbanner.listener.OnItemClickListener;
import com.blankj.utilcode.util.EmptyUtils;
import com.bumptech.glide.Glide;
import com.cxg.fairy.app.AccountManager;
import com.cxg.fairy.delegates.LatteDelegate;
import com.cxg.fairy.ec.main.order.CheckPayDelegate;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.LatteLogger;
import com.cxg.fairy.util.money.MoneyUtil;
import com.diabin.latte.ec.R;
import com.diabin.latte.ec.R2;

import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import butterknife.OnItemSelected;
import butterknife.OnTextChanged;
import butterknife.OnTouch;


/**
 * Created by Administrator on 2018/2/26.
 */

public class DepositDelegate extends LatteDelegate implements OnItemClickListener, ISuccess, View.OnClickListener {

    private static final String ACCOUNT_PHONENO = "ACCOUNT_PHONENO";


    @BindView(R2.id.aiv_deposit_banner)
    AppCompatImageView depositBannerImageView = null;

    @BindView(R2.id.acb_deposit_amount_1)
    AppCompatButton depositAmountA = null;
    @BindView(R2.id.acb_deposit_amount_2)
    AppCompatButton depositAmountB = null;
    @BindView(R2.id.acb_deposit_amount_3)
    AppCompatButton depositAmountC = null;
    @BindView(R2.id.acb_deposit_amount_4)
    AppCompatButton depositAmountD = null;
    @BindView(R2.id.acb_deposit_amount_5)
    AppCompatButton depositAmountE = null;
    @BindView(R2.id.acb_deposit_amount_6)
    AppCompatButton depositAmountF = null;
    @BindView(R2.id.acb_deposit_amount_7)
    AppCompatButton depositAmountG = null;
    @BindView(R2.id.acb_deposit_amount_8)
    AppCompatButton depositAmountH = null;
    @BindView(R2.id.acb_deposit_amount_9)
    AppCompatEditText depositAmountI = null;

    @BindView(R2.id.acb_deposit_action)
    AppCompatButton depositAction = null;
    @BindView(R2.id.atv_deposit_account)
    AppCompatTextView tvDepositAccount = null;

    @Override
    public Object setLayout() {
        return R.layout.delegate_deposit;
    }

    Bundle mBundle = new Bundle();

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @NonNull View rootView) {
        depositAmountA.setOnClickListener(this);depositAmountB.setOnClickListener(this);depositAmountC.setOnClickListener(this);depositAmountD.setOnClickListener(this);depositAmountE.setOnClickListener(this);depositAmountF.setOnClickListener(this);depositAmountG.setOnClickListener(this);depositAmountH.setOnClickListener(this);depositAmountI.setOnClickListener(this);
        depositAction.setEnabled(false);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        RestClient.builder()
                .url("getRecharge")
                .params("uuid",AccountManager.getSignUUID())
                .loader(getContext())
                .success(this)
                .build()
                .post();
    }

    @OnClick(R2.id.back_home)
    void onClickBackHome() {
        getSupportDelegate().pop();
    }

    @Override
    public void onItemClick(int position) {

    }

    public static DepositDelegate create(String account) {
        final Bundle args = new Bundle();
        args.putString(ACCOUNT_PHONENO, account);
        final DepositDelegate delegate = new DepositDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onSuccess(String response) {
        final JSONObject dataObj = JSON.parseObject(response).getJSONObject("data");
        tvDepositAccount.setText(dataObj.getString("accountNo"));
        Glide.with(getContext()).load(dataObj.getString("banner")).into(depositBannerImageView);
    }

    @OnClick(R2.id.acb_deposit_action)
    public void toCashiers() {
        String amount = mBundle.getString("deposit_amount");
        amount = MoneyUtil.formatMoney(amount);
        LatteLogger.e("toCashiers amount = " , amount);

        RestClient.builder()
                .url("placeRechargeOrder")
                .params("uuid",AccountManager.getSignUUID())
                .params("orderAmount",amount)
                .loader(getContext())
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        final JSONObject obj = JSON.parseObject(response).getJSONObject("data");
                        String orderNo = obj.getString("orderNo");
                        getSupportDelegate().startWithPop(new CheckPayDelegate().setIsDeposit(true).setOrderNo(orderNo));
                    }
                })
                .build()
                .post();
    }


    @SuppressWarnings("deprecation")
    @OnTouch(R2.id.acb_deposit_amount_9)
    public boolean selectedEditText() {
        int amount9 = Integer.parseInt(depositAmountI.getText().toString());
        //getFocus
        depositAmountI.setFocusable(true);
        depositAmountI.setFocusableInTouchMode(true);
        depositAmountI.requestFocus();
        //getIme
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getContext().INPUT_METHOD_SERVICE);
        imm.showSoftInput(depositAmountI, InputMethodManager.SHOW_IMPLICIT);
        LatteLogger.e("onItemSelected=",""+amount9);
        changeEditStatus(amount9);
        return true;
    }

    @OnTextChanged(R2.id.acb_deposit_amount_9)
    public void textChangedEditText() {
        if (depositAmountI.hasFocus()){
            int amount9 = Integer.parseInt(depositAmountI.getText().toString());
            LatteLogger.e("textChangedEditText=",""+depositAmountI.getText());
            if (depositAmountI.getText().toString()!= null && EmptyUtils.isNotEmpty(depositAmountI.getText().toString()) && depositAmountI.getText().toString()!=""){
                String amount = depositAmountI.getText().toString();
                Pattern p = Pattern.compile("[^0-9]");
                Matcher m = p.matcher(amount);
                amount = m.replaceAll("").trim();
                mBundle.putString("deposit_amount",amount);
            }
            changeEditStatus(amount9);
        }else{
        }
    }

    public void changeButtonStatus(int editTextLength){
        initAllButtonStatus();
        depositAction.setEnabled(true);
        depositAction.setBackgroundColor(Color.parseColor("#f91313"));
        depositAction.setTextColor(Color.WHITE);
    }

    public void changeEditStatus(int editTextLength){
        if (editTextLength > 0){
            initAllButtonStatus();
            depositAction.setEnabled(true);
            depositAction.setBackgroundColor(Color.parseColor("#f91313"));
            depositAction.setTextColor(Color.WHITE);
        }else if (editTextLength == 0){
            initAllButtonStatus();
            depositAction.setEnabled(false);
            depositAction.setBackgroundColor(Color.parseColor("#c7c6c6"));
            depositAction.setTextColor(Color.BLACK);
        }
    }

    @SuppressLint("NewApi")
    @Override
    public void onClick(View v) {
        initAllButtonStatus();
        if (v.toString().contains("acb_deposit_amount_1")) {
            depositAmountA.setEnabled(true);
            setButtonStatus(depositAmountA);
        } else if (v.toString().contains("acb_deposit_amount_2")) {
            depositAmountB.setEnabled(true);
            setButtonStatus(depositAmountB);
        } else if (v.toString().contains("acb_deposit_amount_3")) {
            depositAmountC.setEnabled(true);
            setButtonStatus(depositAmountC);
        } else if (v.toString().contains("acb_deposit_amount_4")) {
            depositAmountD.setEnabled(true);
            setButtonStatus(depositAmountD);
        } else if (v.toString().contains("acb_deposit_amount_5")) {
            depositAmountE.setEnabled(true);
            setButtonStatus(depositAmountE);
        } else if (v.toString().contains("acb_deposit_amount_6")) {
            depositAmountF.setEnabled(true);
            setButtonStatus(depositAmountF);
        } else if (v.toString().contains("acb_deposit_amount_7")) {
            depositAmountG.setEnabled(true);
            setButtonStatus(depositAmountG);
        } else if (v.toString().contains("acb_deposit_amount_8")) {
            depositAmountH.setEnabled(true);
            setButtonStatus(depositAmountH);
        }
    }

    @SuppressWarnings("deprecation")
    public void initAllButtonStatus() {
        depositAmountA.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountB.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountC.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountD.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountE.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountF.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountG.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountH.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));depositAmountI.setBackground(this.getResources().getDrawable(R.drawable.button_nopress));
        depositAction.setEnabled(false);
        depositAction.setBackgroundColor(Color.parseColor("#c7c6c6"));
        depositAction.setTextColor(Color.BLACK);
    }

    @SuppressWarnings("deprecation")
    public void setButtonStatus(AppCompatButton depositAmount) {
        String amount = depositAmount.getText().toString();
        Pattern p = Pattern.compile("[^0-9]");
        Matcher m = p.matcher(amount);
        amount = m.replaceAll("").trim();
        mBundle.putString("deposit_amount",amount);
//        //clearFocus
        depositAmountI.setFocusableInTouchMode(false);
        depositAmountI.setText("");
        depositAmountI.clearFocus();

        //closeIme
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(getContext().INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(depositAmountI.getWindowToken(), 0);

        depositAction.setEnabled(true);
        depositAction.setBackgroundColor(Color.parseColor("#02a41d"));
        depositAction.setTextColor(Color.WHITE);

        depositAmount.setBackground(this.getResources().getDrawable(R.drawable.button_press));
        LatteLogger.e("setButtonStatus=",depositAmount.getText().toString());
    }



}
